class Hero {
    constructor() {
        this.x = 200;
        this.y = 500;
        this.img = resObj[1];
        this.width = this.img.width;
        this.height = this.img.height;
        this.speed = 5;
        this.rightImgs = resObj.slice(1, 9);
        this.leftImgs = resObj.slice(9, 17);
        this.imgIndex = 0;
        this.direction = 'right';
        this.currentImgs = this.rightImgs;
        this.leftAttackImgs = resObj.slice(17,23);
        this.rightAttackImgs = resObj.slice(23,29);
        this.attackImgIndex = 0;
        this.isAttack = false;
        this.hp = 100;
        this.totalHp = 100;
        this.mp = 0;
        this.totalMp = 100;
        this.headImg = resObj[45];
        this.boomImgs = resObj.slice(46,72);
        this.boomImgIndex = 0;
        this.isBoom = false;
    }
    draw(ctx) { 
        this.move();
        ctx.drawImage(this.headImg, 0, 0,100,100 )

        ctx.strokeRect(120, 10, this.width, 30);
        ctx.fillStyle = 'yellowgreen';
        ctx.fillRect(120, 10, this.width * this.hp / this.totalHp, 30);

        if (this.mp > this.totalMp) {
            this.mp = this.totalMp;
        }
        ctx.strokeRect(120, 60, this.width, 30);
        ctx.fillStyle = 'blue';
        ctx.fillRect(120, 60, this.width * this.mp / this.totalMp, 30);

        if (this.isAttack == true) { //攻击
            if (this.direction == "left") { 
                ctx.drawImage(this.leftAttackImgs[parseInt(this.attackImgIndex)], this.x,this.y, this.width, this.height);
            } else if (this.direction == "right") {
                ctx.drawImage(this.rightAttackImgs[parseInt(this.attackImgIndex)], this.x, this.y, this.width, this.height);
            } 
            this.attackImgIndex += 0.4; //防止索引越界
            if (this.attackImgIndex >= this.rightAttackImgs.length) { 
                this.attackImgIndex = 0; //让自己攻击的状态消失，恢复到正常行走的状态
                this.isAttack = false;
            }
        } else if(this.isBoom == true && this.mp == this.totalMp) {
                ctx.drawImage(this.boomImgs[parseInt(this.boomImgIndex)], this.x, this.y, this.boomImgs[0].width, this.boomImgs[0].height);
                this.boomImgIndex += 0.5;
                if (this.boomImgIndex >= this.boomImgs.length) {
                    this.boomImgIndex = 0;
                    this.isBoom = false;
                    this.mp = 0;
                }
        } else {
        //行走
            ctx.drawImage(this.currentImgs[parseInt(this.imgIndex)], this.x, this.y, this.width, this.height);
        } 

   
        
    }
    move () {
        //  移动不超出屏幕 两种方法
        if(keySet.has(65) && this.x > 0) {
            this.x -= this.speed;
            this.currentImgs = this.leftImgs;
            this.direction = 'left';
            // if (this.x <= 0) {
            //     this.x = 0;
            // }
        } else if(keySet.has(68) && this.x < screen.availWidth - this.width) {
            this.x += this.speed;
            this.currentImgs = this.rightImgs;
            this.direction = 'right';
            // if (this.x >= screen.availWidth - this.width) {
            //     this.x = screen.availWidth - this.width;
            // }
        } 
        if (keySet.has(87) && this.y > 300) { 
            this.y -= this.speed;
            // if (this.y <= 300) {
            //     this.y = 300;
            // }
        } else if (keySet.has(83) && this.y < 720 - this.height) {
            this.y += this.speed;
            // if (this.y >= 720 - this.height) {
            //     this.y = 720 - this.height;
            // }
        }
        if (keySet.has(65) || keySet.has(68) || keySet.has(87) || keySet.has(83)) {
            this.imgIndex += 0.5;
			//索引不能大于数组的长度
			if (this.imgIndex >= this.rightImgs.length) {
				this.imgIndex = 0;
			}
		}
    }
}
